package com.lzf.config;


import com.lzf.filter.CheckClientParameterFilter;
import com.lzf.filter.KaptchaCodeFilter;
import com.lzf.provider.MyUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

/**
 * @EnableGlobalMethodSecurity(prePostEnabled = true) 启用方法级别的认证
 *     prePostEnabled = true 表示可以使用 @PreAuthorize注解 和 @PostAuthorrize
 */
@Configuration
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;
    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //添加自定义实现 MyUserDetailsService
        auth
                .userDetailsService(myUserDetailsService())
                .passwordEncoder(passwordEncoder());

    }




    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterBefore(kaptchaCodeFilter(), BasicAuthenticationFilter.class)
                .csrf().disable()
                //放开授权框架自带接口TokenEndpoint等终点接口
                .authorizeRequests()
                .antMatchers("/**").permitAll().and()
                //取消表单登录
                .formLogin().disable()
                //取消session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .logout()
                .logoutSuccessHandler(logoutSuccessHandler)

                //.clearAuthentication(true)
                //.invalidateHttpSession(true)
                //.deleteCookies("JSESSIONID")
        ;
    }

    @Bean
    public KaptchaCodeFilter kaptchaCodeFilter() {
        return new KaptchaCodeFilter();
    }

    //密码模式需要使用到 默认验证管理器
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    //使用自定义userDetailsService
    @Bean
    public UserDetailsService myUserDetailsService() {
        return new MyUserDetailService();
    }

    //加密模式
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


}
